package com.ruoyi.web.controller.trackApply;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.constant.ReturnCodeConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ProcessStopException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.SseEmitterUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.dm.domain.po.TrackTleParam;
import com.ruoyi.dm.service.ICollectTleRecordService;
import com.ruoyi.trackapply.OrbitForcastService;
import com.ruoyi.trackapply.domain.OrbitForcastParam;
import com.ruoyi.trackapply.domain.OrbitForcastVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.servlet.http.HttpServletResponse;
import java.io.File;


/**
 * <p>
 *  典型轨道大气应用-不同磁暴期大气模式预报效应评估
 * </p>
 */
@RestController
@RequestMapping("/orbit/forcast")
@Slf4j
@Api(tags = "典型轨道大气应用-不同磁暴期大气模式预报效应评估")
public class OrbitForcastController {

    @Autowired
    private OrbitForcastService orbitService;

    @Autowired
    private ICollectTleRecordService collectTleRecordService;


    @ApiOperation("计算")
    @PostMapping("/cal")
    public R<OrbitForcastVO> cal(@Validated @RequestBody OrbitForcastParam param) throws Exception {

        LoginUser loginUser = SecurityUtils.getLoginUser();
        String sseKey = param.getSseId() + loginUser.getUsername();
        try {
            param.setSseId(sseKey);
            OrbitForcastVO vo = orbitService.cal(param);
            return R.ok(vo);
        }catch (ProcessStopException pe) {
            return R.fail(ReturnCodeConstants.PORCESS_STOP,"任务模型评估计算终止");
        }catch (Exception e) {
            log.error("",e);
            return R.fail(e.getMessage());
        }finally {
            SseEmitterUtils.sendMessage(sseKey,"100");
            SseEmitterUtils.remove(sseKey);
        }
    }

    @ApiOperation(value = "建立连接", notes = "建立连接")
    @GetMapping(path = "/subscribe", produces = {MediaType.TEXT_EVENT_STREAM_VALUE})
    public SseEmitter push(String id, String flag) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return SseEmitterUtils.connect(id+loginUser.getUsername(), flag);
    }

    @ApiOperation("结果入库")
    @PostMapping("/save")
    public R save(@Validated @RequestBody OrbitForcastParam param) {
        orbitService.save(param);
        return R.ok();
    }

    @ApiOperation("结果下载")
    @PostMapping("/download")
    public void download(@Validated @RequestBody OrbitForcastParam param, HttpServletResponse response) {
        try {
            File file = orbitService.download(param);
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, file.getName());
            FileUtils.writeBytes(file.getAbsolutePath(), response.getOutputStream());
        }catch (Exception e) {
            log.error("下载失败",e);
        }
    }

    @ApiOperation("检索目标")
    @PostMapping("/query")
    public R<IPage> query( @RequestBody TrackTleParam param) {

        IPage iPage = collectTleRecordService.queryByParam(param);
        return R.ok(iPage);
    }



}
